package com.sunday.volunteer.admin.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.GroupType;
import com.sunday.volunteer.admin.http.VolunteerClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 创建组织
* */
public class CrateGroupActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.spinner_type)
    Spinner spinnerType;
    @Bind(R.id.et_name)
    ClearEditText etName;
    @Bind(R.id.et_for)
    ClearEditText etFor;
    @Bind(R.id.et_team)
    ClearEditText etTeam;
    @Bind(R.id.et_proName)
    ClearEditText etProName;
    @Bind(R.id.et_proPhone)
    ClearEditText etProPhone;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.btn_submit)
    Button btnSubmit;
    @Bind(R.id.tv_getCode)
    TextView tvGetCode;

    private TimeCount timeCount;
    private ArrayAdapter<String> adapter;
    private List<GroupType> groupTypeList;
    private List<String> list = new ArrayList<String>();
    private boolean isLoadType;//是否加载过组织类型

    private String groName,groupFor,groType,team,proName,proPhone,code;
    private int teamNums;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crate_group);
        ButterKnife.bind(this);
        titleView.setText("创建组织");
        initView();
        getType();
    }

    private void initView() {
        timeCount = new TimeCount(60000, 1000, tvGetCode);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerType.setAdapter(adapter);
        spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                groType = groupTypeList.get(position).getName();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.tv_getCode,R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_getCode:
                proPhone = etProPhone.getText().toString().trim();
                if (StringUtils.isMobileNO(proPhone)){
                    getCode(proPhone);
                }else {
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                }
                break;
            case R.id.btn_submit:
                groName = etName.getText().toString().trim();
                groupFor = etFor.getText().toString().trim();
                team = etTeam.getText().toString().trim();
                proName = etProName.getText().toString().trim();
                proPhone = etProPhone.getText().toString().trim();
                code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(groType)){
                    ToastUtils.showToast(mContext,"请选择组织类型");
                    return;
                }else if (TextUtils.isEmpty(groName)){
                    ToastUtils.showToast(mContext,"组织全称不能为空");
                    return;
                }else if (TextUtils.isEmpty(groupFor)){
                    ToastUtils.showToast(mContext,"服务范围不能为空");
                    return;
                }else if (TextUtils.isEmpty(team)){
                    ToastUtils.showToast(mContext,"团队规模不能为空");
                    return;
                }else if (TextUtils.isEmpty(proName)){
                    ToastUtils.showToast(mContext,"负责人姓名不能为空");
                    return;
                }else if (TextUtils.isEmpty(proPhone)){
                    ToastUtils.showToast(mContext,"负责人手机号不能为空");
                    return;
                }else if (!StringUtils.isMobileNO(proPhone)){
                    ToastUtils.showToast(mContext,"请输入正确的手机号");
                    return;
                }else if (TextUtils.isEmpty(code)){
                    ToastUtils.showToast(mContext,"验证码不能为空");
                    return;
                }
                teamNums = Integer.parseInt(team);
                if (teamNums<=0){
                    ToastUtils.showToast(mContext,"团队规模只能为正整数");
                    return;
                }
                applyGroup();
                //checkCode(proPhone,code);
                break;
        }
    }

    //组织类型
    private void getType(){
        showLoadingDialog(0);
        Call<ResultDO<List<GroupType>>> call = VolunteerClient.getVolunteerAdapter().select("groupType");
        call.enqueue(new Callback<ResultDO<List<GroupType>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<GroupType>>> call, Response<ResultDO<List<GroupType>>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    groupTypeList = response.body().getResult();
                    list.clear();
                    for (int i = 0;i<groupTypeList.size();i++){
                        list.add(groupTypeList.get(i).getName());
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<GroupType>>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    //获取验证码
    private void getCode(String phone) {
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().sendCode(phone);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    //校验验证码
    private void checkCode(final String phone, final String code) {
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().checkCode(phone, code);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    applyGroup();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    //申请创建组织
    private void applyGroup(){
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().apply(proPhone,groName,proName,groType,groupFor,teamNums, code);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }
}
